package com.adapter;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

//import android.R.integer;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.bean.BResource;
import com.bean.Down;
import com.constant.Constant;
import com.king.MainActivity;
import com.king.SelectTeach;
import com.kingPadStudy.activity.KingPadStudyActivity;
import com.kingpad.KingPadApp;
import com.kingpad.R;
import com.net.LoadData;
import com.net.RequestListener;
import com.net.RequestParameter;
import com.server.data.DownloadData;
import com.server.data.DownloadPackageData;
import com.utils.Util;


/**
 * 每一种资源
 * @author <a href="mailto:udmjh0508@126.com">莫建华</a>
 * @created 2013-1-14
 * @version V-1.0
 */
@TargetApi(9)
public class ResourceAdapter extends BaseAdapter {
	private static String TAG = "ResourceAdapter";
	/** 
	 * 用于保存资源是否下载  
	 */ 
	private static HashMap<String, BResource> map = new HashMap<String, BResource>();
	private static HashSet<String> mSet = new HashSet<String>();
	private Context mContext;
	private LayoutInflater mInflater;
	private ArrayList<BResource> mList;
	private static DownloadManager mDownloadManager;
	/**
	 * 该课程跟的目录
	 */
	/**
	 * 课程id号
	 */
	private String courseId;
	/**
	 * 课程名Math,Chinese,English
	 */
	private String course;
	/**
	 * 年级名 , '一年级，二年级'
	 */
	private String grade;
	/*
	 * 正在解压的资源
	 */
	private int number_unzip = 0;
	/** 
	 * 年级id号  
	 */	
	private String gradeId;  
	final static int UNZIP_ERROR = 1;  
	final static int UNZIP_SUCCESS = 2;  
	final static int DOWNLOAD_STATING = 3;   
	final static Lock lock = new ReentrantLock();     
	private ProgressBar  _ProgressBar01;       
	private ProgressBar  _ProgressBar02; 
	private LinearLayout linear_1; 
	private LinearLayout linear_2;
	private Button btn_download ;
	private int id_download = 0;
	private String url_download ;
	private int id_get = 1;
	private int state_download = 0; 
	private boolean  isSecondDownload = false; 
	
	public ResourceAdapter(final Context context, ArrayList<BResource> list,   
			String course, String courseId, String grade, String gradeId) {  
		this.mContext = context;
		mInflater = LayoutInflater.from(mContext);
		if(list == null)
			mList = new ArrayList<BResource>();
		else 
			mList = list;
		mDownloadManager = (DownloadManager) mContext.getSystemService(Activity.DOWNLOAD_SERVICE);
		this.courseId = courseId;
		this.course = course;
		this.grade = grade;
		this.gradeId = gradeId;
		myHandler2 = new Handler(){
	    	public void handleMessage(android.os.Message msg) {
	    		//获得进度，该进度由实际的操作进行通知。这里负责对通知进行处理
	    		int progress = msg.arg1;
	    		//设置进度条的当前位置
	    		_ProgressBar02.setProgress(progress);
	    		if(progress == 100)
	    		{
//	    			Toast.makeText(context, "下载成功", 0).show();
	    			_ProgressBar02.setVisibility(View.INVISIBLE);
	    			handleDownLoad();
	    		}
	    	};
	    };
		myHandler = new Handler(){
	    	public void handleMessage(android.os.Message msg) {
	    		//获得进度，该进度由实际的操作进行通知。这里负责对通知进行处理
	    		int progress = msg.arg1;
	    		//设置进度条的当前位置
	    		_ProgressBar01.setProgress(progress);
	    		if(progress == 100)
	    		{
	    			_ProgressBar01.setVisibility(View.INVISIBLE);
	    			handleDownLoad();
	    		}
	    	};
	    };
	    System.out.println("在初始化后，mList的大小："+mList.size());
	}
	
	private void handleDownLoad(){
		Toast.makeText(mContext, "文件下载成功, 开始解压文件", 1).show();
		Log.i("tar", "开始解压解压文件");
		KingPadStudyActivity.number_unzip += 1; 
		System.out.println("number_unzip++，number_unzip="+KingPadStudyActivity.number_unzip);
		BResource resource = null;
		System.out.println("准备获取资源："+id_get);
		System.out.println("mList.size()="+mList.size());
		if(state_download == 1){
			//只有资源，没package
			linear_1.setVisibility(View.GONE);
			linear_2.setVisibility(View.GONE);
			if(isSecondDownload){
				resource = map.get("p");
			}else{
				resource = map.get("r");
			}
		}else {
			resource = map.get("r");
			linear_1.setVisibility(View.GONE);
			state_download = 1;
			isSecondDownload = true;
		}
		String path = Constant.DOWNLOAD_PATH + "/" + resource.getResourceNumber() + "_" + resource.getFileName();
		unZip(path, resource);
		synchronized (KingPadStudyActivity.sDownloading) {
			//TODO 强制转换
			KingPadStudyActivity.sDownloading -= 1;
			KingPadStudyActivity activity = (KingPadStudyActivity) mContext;
			activity.setProgressText(); 
		}
	}
	
	
	
	public int getCount() {
		return mList.size();
	}
	
	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		final BResource resource = mList.get(position);
		viewHolder = new ViewHolder();
		convertView = mInflater.inflate(R.layout.resource_item, null);
		viewHolder.title = (TextView) convertView.findViewById(R.id.textview_file_name);
		viewHolder.price = (TextView) convertView.findViewById(R.id.textview_price);
		viewHolder.space = (TextView) convertView.findViewById(R.id.textview_space);
		viewHolder.download = (Button) convertView.findViewById(R.id.button_download);
		viewHolder.isBuy = (TextView) convertView.findViewById(R.id.textview_is_buy);
		viewHolder.progressBar1 = (ProgressBar)convertView.findViewById(R.id.ProgressBar01);
		viewHolder.progressBar2 = (ProgressBar)convertView.findViewById(R.id.ProgressBar02);
		viewHolder.layout_1 = (LinearLayout)convertView.findViewById(R.id.linear_progress_1);
		viewHolder.layout_2 = (LinearLayout)convertView.findViewById(R.id.linear_progress_2);
		_ProgressBar01 = viewHolder.progressBar1;
		_ProgressBar02 = viewHolder.progressBar2;
		linear_1 = viewHolder.layout_1;
		linear_2 = viewHolder.layout_2;
		btn_download = viewHolder.download ;
		//System.out.println(resource.getResourceName());
		viewHolder.title.setText("资源名：" + resource.getResourceName());
		viewHolder.price.setText("价格：" + resource.getResourcePrice());
		int space = (int)(resource.getResourceSpace() * 1.0 / (1048576));
		viewHolder.space.setText("资源大小:" + space + "M");
		if(resource.getDownload() == 1) {
			viewHolder.isBuy.setText("已购买");
		} else {
			viewHolder.isBuy.setText("未购买");
		} 
		viewHolder.download.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				//TODO 点击事件
				showDialog(resource);
			}
		});
		return convertView;
	}
	
	 private Handler myHandler;
	 private Handler myHandler2;
	
	
	
	/**
	 * 显示一个确认对话框
	 */
	private void showDialog(final BResource resource) {
		//TODO 弹出对话
		View view = mInflater.inflate(R.layout.dlg_sure, null);
		TextView textView = (TextView) view.findViewById(R.id.textview_text);
		String text = null;
		if(resource.getDownload() == 1) {
			text = "该资源已下载，下载该资源不扣费";
		}
		else {
			text = "下载该资源将会被扣除" + resource.getResourcePrice() + "元费用";
		}
		textView.setText(text);
		Dialog dlg = new AlertDialog.Builder(mContext)
		.setTitle("提示")
		.setView(view)
		.setNeutralButton("是", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				Log.i("resourceAdapter", "开始下载资源");
				Message msg = new Message();
				msg.what = DOWNLOAD_STATING;
				msg.obj = resource;
				handler.sendMessage(msg);
				btn_download.setClickable(false);
			}
		})
		.setNegativeButton("否", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) { }
		}).create();
		dlg.show();
	}
	
	
	static class ViewHolder {
		TextView title;
		TextView price;
		TextView space;
		TextView isBuy;
		Button download;
		ProgressBar progressBar1;
		ProgressBar progressBar2;
		LinearLayout layout_1;
		LinearLayout layout_2;
	}
	
	/**
	 * 下载资源
	 * @param resource : 资源信息
	 * @param id ： 资源id号
	 * @param url ： 资源下载链接
	 * @param name ： 资源名,该资源必须得为zip文件
	 */
	private void download(final BResource resource,String url) {
		System.out.println("进入download...双残,");
	    System.out.println("mList的大小："+mList.size());
//		DownloadManager.Request down=new DownloadManager.Request (Uri.parse(resource.getResourceUrl()));  
//        down.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE|DownloadManager.Request.NETWORK_WIFI);  
//        down.setShowRunningNotification(true);  
//        down.setVisibleInDownloadsUi(true);  
        // 创建目录
        File file = new File(Constant.DOWNLOAD_PATH);
        if(!file.exists())
        	file.mkdirs();
        // 以前下载过该文件时删除该文件
        file = new File(Constant.DOWNLOAD_PATH + "/" + resource.getResourceNumber() + "_" + resource.getFileName());
        if(file.exists())
        	file.delete();
//        down.setShowRunningNotification(true);  
//        down.setVisibleInDownloadsUi(true);  
//        down.setDestinationInExternalPublicDir(Constant.DOWNLOAD_PATH_PARAM, resource.getResourceNumber() + "_" + resource.getFileName());
//        long downloadId = mDownloadManager.enqueue(down);  
//        resource.setId(downloadId);
//        Log.i("downloadId", downloadId + ",set");
        
//      //先置为0//TODO 进度条
//		_ProgressBar01.setProgress(0);
		//要下载的文件路径
		String urlDownload = "";
		//urlDownload =  "http://192.168.3.39/text.txt";
		urlDownload = url;
//		// 获得存储卡路径，构成 保存文件的目标路径
//		String dirName = "";
//		dirName = Environment.getExternalStorageDirectory()+"/MyDownload/";
//		File f = new File(dirName);
//		if(!f.exists())
//		{
//			f.mkdir();
//		}
		//启动文件下载线程
		linear_1.setVisibility(View.VISIBLE);
		new FileDownloader(urlDownload,file,myHandler).start();
//		id_download ++;
		System.out.println("id_download++...id_download="+id_download);
		resource.setId(id_download);
		map.put("r", resource);
		BResource r = map.get("r");
		System.out.println("放入MAP r：map.get(r)="+map.get("r"));
		synchronized (KingPadStudyActivity.sDownloading) {
			KingPadStudyActivity.sDownloading += 1;
			KingPadStudyActivity activity = (KingPadStudyActivity) mContext;
			activity.setProgressText();
		}
//        return downloadId;
	}
	
	
//	
//	private BroadcastReceiver receiver = new BroadcastReceiver() { 
//	    @Override 
//	    public void onReceive(Context context, Intent intent) {
//	    	long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
//	    	Log.i("download_id", downloadId + ",get");
//	        String action = intent.getAction();
//        	DownloadManager.Query query = new DownloadManager.Query();
//        	query.setFilterById(downloadId);
//        	Cursor c = mDownloadManager.query(query);
//        	if(c.moveToFirst()) {
//        		int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
////        		if(status == DownloadManager.STATUS_FAILED) {
////        			Toast.makeText(mContext, "文件下载失败", 1).show();
////        			synchronized (KingPadStudyActivity.sDownloading) {
////						KingPadStudyActivity.sDownloading -= 1;
////						
////						KingPadStudyActivity activity = (KingPadStudyActivity) mContext;
////						activity.setProgressText(); 
////					}
////        		} // if
////        		else 
//        		if(status == DownloadManager.STATUS_SUCCESSFUL) {
//        			Toast.makeText(mContext, "文件下载成功, 开始解压文件", 1).show();
//        			Log.i("tar", "开始解压解压文件");
//        			BResource resource = null;
//        			for(int i = 0, len = mList.size(); i < len; i++) {
//        				BResource res = mList.get(i);
//        				if(res.getId() == downloadId) {
//        					resource = res;
//        					break;
//        				}
//        					
//        			}
//        			
//        			if(resource == null) {
//        				resource = map.get(downloadId);
//        			}
//        			
//        			if(resource != null) {
//        				Log.i(TAG, resource.getFileName());
//        				String path = Constant.DOWNLOAD_PATH + "/" + resource.getResourceNumber() + "_" + resource.getFileName();
//        				unZip(path, resource);
//        			}
//        			
//        			synchronized (KingPadStudyActivity.sDownloading) {
//        				//TODO 强制转换
//						KingPadStudyActivity.sDownloading -= 1;
//						
//						KingPadStudyActivity activity = (KingPadStudyActivity) mContext;
//						activity.setProgressText(); 
//					}
//        			
//        		} // else 
//        		else {
//        			Toast.makeText(mContext, "文件下载失败", 1).show();
//        			synchronized (KingPadStudyActivity.sDownloading) {
//						KingPadStudyActivity.sDownloading -= 1;
//						
//						KingPadStudyActivity activity = (KingPadStudyActivity) mContext;
//						activity.setProgressText(); 
//					}
//        		}
//        	}
//	        	
//	    } // onReceive
//	}; 
//	
	/**
	 * 开启线程加压文件
	 * @param path : zip文件完全路径
	 * @param resourceName : 资源的名 aa.zip
	 */
	private void unZip(final String path, final BResource resource) {
		new Thread(new Runnable() {
			public void run() {
				try {
					String resourcePath;
					resourcePath = Constant.RESOURCE_PATH + "/" +  course + "/" + grade;
					System.out.println("准备解压资源：URL="+resourcePath);
					File file = new File(resourcePath);
					if(file.exists())
						file.delete();
					Log.i(TAG, "当前文件路径：" + resourcePath);
					Util.readByApacheZipFile(path, resourcePath);
					Message msg = new Message();
					msg.what = UNZIP_SUCCESS;
					msg.obj = path;
					handler.sendMessage(msg);
					// 解压成功之后，把数据对象保存到db文件中
					Down down = new Down(course, grade, resource.getIspackage(), resource.getResourceName(), resource.getResourceId());
					MainActivity.db.addResource(down);
					// 数据加载列表中
					if(down.getIsPackage() == 1)
						return;
					HashMap<String, ArrayList<String>> map = SelectTeach.mHashMap.get(down.getCourse());
					if(map == null) {
						map = new HashMap<String, ArrayList<String>>();
						SelectTeach.mHashMap.put(down.getCourse(), map);
					}
					ArrayList<String> list = map.get(down.getGrade());
					if(list == null) {
						list = new ArrayList<String>(4);
						map.put(down.getGrade(), list);
					}
					int i = 0, len = list.size();
					for( i = 0; i <= len; i++) {
						if(i == len) {
							list.add(down.getResourceName());
							break;
						}
						
						if(down.getResourceName().equals(list.get(i))) 
							break;
					}
					
				} catch (Exception e) {
					e.printStackTrace();
				}
				finally {
//					synchronized (KingPadStudyActivity.sDownloading) {
//						KingPadStudyActivity.sDownloading -= 1;
//						
////						KingPadStudyActivity activity = (KingPadStudyActivity) mContext;
////						activity.setProgressText(); 
//					}
					handler.sendEmptyMessage(100);
				}
				//mSet.remove(resource);
			}
		}).start(); 
	} // unZip
	
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch(msg.what) {
			case UNZIP_ERROR:
				KingPadStudyActivity.number_unzip -= 1 ;
				String text = "解压文件失败, 文件路径为：" + (String)msg.obj + "\n 你可以手动解压到该教材对应目录";
				System.out.println("number_unzip--，number_unzip="+KingPadStudyActivity.number_unzip);
				Toast.makeText(mContext, text, 1).show();
//				synchronized (KingPadStudyActivity.sDownloading) {
//					KingPadStudyActivity.sDownloading -= 1;
//					KingPadStudyActivity activity = (KingPadStudyActivity) mContext;
//					activity.setProgressText(); 
//				}
				synchronized (KingPadStudyActivity.number_unzip) {
					KingPadStudyActivity activity = (KingPadStudyActivity) mContext;
					activity.setProgressText(); 
				}
				break;
			case UNZIP_SUCCESS:
				KingPadStudyActivity.number_unzip -= 1 ;
				System.out.println("number_unzip--，number_unzip="+KingPadStudyActivity.number_unzip);
				
				Toast.makeText(mContext, "解压文件成功!", 1).show();
				synchronized (KingPadStudyActivity.number_unzip) {
					KingPadStudyActivity activity = (KingPadStudyActivity) mContext;
					activity.setProgressText(); 
				}
				break;
			case DOWNLOAD_STATING:
				KingPadStudyActivity.number_unzip -= 1 ;
//				download((BResource) msg.obj);
//				boolean b = packageExists();
//				if(b == false) {
//					//download(resource)
//				}
				System.out.println("number_unzip--，number_unzip="+KingPadStudyActivity.number_unzip);
				synchronized (KingPadStudyActivity.number_unzip) {
					KingPadStudyActivity activity = (KingPadStudyActivity) mContext;
					activity.setProgressText(); 
				}
				downloadResource((BResource) msg.obj);
				break;
				
//			case 100:
//				synchronized (KingPadStudyActivity.sDownloading) {
//				KingPadStudyActivity.sDownloading -= 1;
//				
//				KingPadStudyActivity activity = (KingPadStudyActivity) mContext;
//				activity.setProgressText(); 
//			}
//				break;
			}
		};
	};

	public void downloadResource(final BResource resource) {
		KingPadStudyActivity activity = (KingPadStudyActivity) mContext;
		final KingPadApp application = (KingPadApp) activity.getApplication();
		RequestParameter parameter = new RequestParameter();
		parameter.add("productNumber", application.getProductNumber());
		parameter.add("resourceId", resource.getResourceId());
		LoadData.loadData(Constant.DOWNLOAD_DATA, parameter, new RequestListener() {
			
			public void onError(String errMsg) {
				Toast.makeText(mContext, "下载当前数据包失败！", 0).show();
			}
			
			public void onComplete(Object obj) {
				DownloadData data = (DownloadData) obj;
				int status = data.getStatus();
				if(status == 0) {
					Toast.makeText(mContext, "余额不足，请充值", 0).show();
					return;
				}
				else if(status == 1) {
					Toast.makeText(mContext, "资源已被删除", 0).show();
					return;
				}
				else if(status == 2) {
					isSecondDownload = false;
					KingPadStudyActivity.number_unzip = 0;
					String url = data.getUrl();
					url_download = url;
					System.out.println("准备下载资源：URL="+url);
					resource.setResourceUrl(url);
					download(resource,url);
					boolean b = packageExists();
					if(b == false) {
						downloadPackage(application.getProductNumber());
						Log.i("tag", "下载package资源");
						state_download = 2;
					}else{
						state_download = 1;
					}
				}
				else {
					Toast.makeText(mContext, "资源下载失败！", 0).show();
				}
			}
		});
		
	}
	
	
	
	/**
	 * 判断该目录下是否存在package包
	 * @return : 是否存在package目录
	 */
	private boolean packageExists() {
		File file = new File(Constant.RESOURCE_PATH + "/" + course + "/" + grade);
		String[] fi = file.list();
		if(fi == null)
			return false;
		
		for(int i = 0; i < fi.length; i++)
			if("package".equals(fi[i]) || "Package".equals(fi[i])) {
				return true;
			}
		
		return false;
	}
	
	/**
	 * 下载package包信息
	 * @param productNumber
	 * @param courseId
	 */
	private void downloadPackage(final String productNumber) {
		System.out.println("进入downloadPackage....");
	    System.out.println("mList的大小："+mList.size());
		if(courseId == null) {
			Log.i("tag", "courseId为null");
			return;
		}
		RequestParameter parameter = new RequestParameter();
		parameter.add("productNumber", productNumber);
		parameter.add("courseId", courseId);
		parameter.add("gradeId", gradeId);
		System.out.println("准备加载。。。");
		LoadData.loadData(Constant.DOWNLOAD_PACKAGE_DATA, parameter, new RequestListener() {
			public void onError(String errMsg) {
				//TODO 弹出对话
				View view = mInflater.inflate(R.layout.dlg_sure, null);
				TextView textView = (TextView) view.findViewById(R.id.textview_text);
				String text = "错误描述："+errMsg;
				textView.setText(text);
				Dialog dlg = new AlertDialog.Builder(mContext)
				.setTitle("提示")
				.setView(view)
				.setNeutralButton("是", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) { 
					}
				}) .create();
				dlg.show();
			}
			public void onComplete(Object obj) {
				DownloadPackageData data = (DownloadPackageData) obj;
				BResource resource = data.getResource();
				resource.setIspackage(1);
		      // 创建目录
		      File file = new File(Constant.DOWNLOAD_PATH);
		      if(!file.exists())
		      	file.mkdirs();
		      // 以前下载过该文件时删除该文件
		      file = new File(Constant.DOWNLOAD_PATH + "/" + resource.getResourceNumber() + "_" + resource.getFileName());
		      if(file.exists())
		      	file.delete();
				//要下载的文件路径
				String urlDownload = "";
				urlDownload = resource.getResourceUrl();
				//启动文件下载线程
				linear_2.setVisibility(View.VISIBLE);
				System.out.println("准备下载package资源：URL="+urlDownload);
				new FileDownloader(urlDownload,file,myHandler2).start();
//				id_download ++;
				map.put("p", resource);
				System.out.println("放入MAP p：map.get(p)="+map.get("p"));
////				try {
//					if(!mSet.contains(course + gradeId)) {
//						mSet.add(course + gradeId);
//						map.put((long) id_download, resource);
//					}
////				}
//				finally {
//					lock.unlock();
//				}
				System.out.println("id_download="+id_download);
				resource.setId(id_download);
				synchronized (KingPadStudyActivity.sDownloading) {
					KingPadStudyActivity.sDownloading += 1;
					KingPadStudyActivity activity = (KingPadStudyActivity) mContext;
					activity.setProgressText();
				}
				lock.lock();
				
			}
		});
	}

//
//	protected long download(BResource resource) {
//
//		System.out.println("进入download单灿");
////		DownloadManager.Request down=new DownloadManager.Request (Uri.parse(resource.getResourceUrl()));  
////      down.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE|DownloadManager.Request.NETWORK_WIFI);  
////      down.setShowRunningNotification(true);  
////      down.setVisibleInDownloadsUi(true);  
//      // 创建目录
//      File file = new File(Constant.DOWNLOAD_PATH);
//      if(!file.exists())
//      	file.mkdirs();
//      // 以前下载过该文件时删除该文件
//      file = new File(Constant.DOWNLOAD_PATH + "/" + resource.getResourceNumber() + "_" + resource.getFileName());
//      if(file.exists())
//      	file.delete();
////      down.setShowRunningNotification(true);  
////      down.setVisibleInDownloadsUi(true);  
////      down.setDestinationInExternalPublicDir(Constant.DOWNLOAD_PATH_PARAM, resource.getResourceNumber() + "_" + resource.getFileName());
////      long downloadId = mDownloadManager.enqueue(down);  
////      resource.setId(downloadId);
////      Log.i("downloadId", downloadId + ",set");
//      
////    //先置为0//TODO 进度条
////		_ProgressBar01.setProgress(0);
//		//要下载的文件路径
//		String urlDownload = "";
//		//urlDownload =  "http://192.168.3.39/text.txt";
//		urlDownload = resource.getResourceUrl();
////		// 获得存储卡路径，构成 保存文件的目标路径
////		String dirName = "";
////		dirName = Environment.getExternalStorageDirectory()+"/MyDownload/";
////		File f = new File(dirName);
////		if(!f.exists())
////		{
////			f.mkdir();
////		}
//		//启动文件下载线程
//		_ProgressBar01.setVisibility(View.VISIBLE);
//		new FileDownloader(urlDownload,file,myHandler).start();
//		id_download ++;
//		resource.setId(id_download);
//		synchronized (KingPadStudyActivity.sDownloading) {
//			KingPadStudyActivity.sDownloading += 1;
//			KingPadStudyActivity activity = (KingPadStudyActivity) mContext;
//			activity.setProgressText();
//		}
//		return id_download;
////      return downloadId;
//	}

}
